﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Questionnaire;
using ZHDJ.WebPage;

namespace ZHDJ.Web.Portal.Questionnaire
{
    public partial class Statistics : PortalPage
    {
        #region  属性

        /// <summary>
        /// 字母数组
        /// </summary>
        public string[] _leter = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M" };


        /// <summary>
        /// 问卷ID
        /// </summary>
        public string QuestionnaireID
        {
            get { return this.Request["id"] ?? "null"; }

        }

        /// <summary>
        /// 题目选项（做题前）
        /// </summary>
        private DataSet _TestSubject;

        /// <summary>
        /// 题目选项（做题前）
        /// </summary>
        public DataSet TestSubject
        {
            get
            {
                if (_TestSubject == null)
                {
                    _TestSubject = PartyQuestionnaire.GetPartyQuestionnaireSubjectTest(QuestionnaireID);
                    _TestSubject.Tables[1].Columns.Add("CategoryName", typeof(string));
                    _TestSubject.Tables[1].Columns.Add("SID", typeof(string));

                }
                return _TestSubject;
            }
        }


        public PartyQuestionnaire q { get; set; }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(QuestionnaireID))
            {
                RegisterJs("loadError()");
            }

            q = DB.PartyQuestionnaires.Find(QuestionnaireID);
            if (q == null || string.IsNullOrEmpty(q.Title))
            {
                RegisterJs("loadError()");
            }

            CheckLogin(true);
            
            
            if (!IsPostBack)
            {
                hdQuestionnaireID.Value = QuestionnaireID;
                lblTitle.Text = q.Title+string.Format("（调查人数：{0}人）",DB.PartyQuestionnaireRecords.Count(a=>a.QuestionnaireID==QuestionnaireID));
                LoadData();
            }
        }

        #region 数据加载

        public void LoadData()
        {
            //加载题目
            var dv = TestSubject.Tables[0].AsDataView();
            dv.Sort = "OrderNo";

            this.rptSubject.DataSource = dv;
            this.rptSubject.DataBind();

            foreach (DataRowView item in dv)
            {
                this.hdSubjectIDs.Value += Convert.ToString(item["ID"]) + ",";
            }
        }

        #endregion


        #region 事件

        /// <summary>
        /// 绑定选项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rptSubject_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem ||
                e.Item.ItemType == ListItemType.SelectedItem)
            {
                //加载选项
                var data = e.Item.DataItem;
                Repeater Repeater = e.Item.FindControl("rptOptions") as Repeater;
                HiddenField hiddenField = e.Item.FindControl("hdSubjectID") as HiddenField;
                HiddenField CategoryName = e.Item.FindControl("hdCategoryName") as HiddenField;
                if (Repeater == null)
                {
                    return;
                }
                if (IsPostBack)
                {

                }
                else
                {
                    //答题前
                    var dt = TestSubject.Tables[1];
                    var formNames = new List<string>();
                    var index = 0;
                    foreach (DataRow dr in dt.Rows)
                    {
                        if (dr["SubjectID"].ToString() != hiddenField.Value)
                        {
                            continue;
                        }
                        dr["CategoryName"] = CategoryName.Value;
                        dr["SID"] = dr["ID"];
                        if (CategoryName.Value == "radio")
                        {
                            dr["ID"] = hiddenField.Value;
                        }
                        if (!formNames.Contains(hiddenField.Value + "_" + CategoryName.Value))
                        {
                            formNames.Add(dr["ID"] + "_" + CategoryName.Value);
                        }
                        if (dr["IsTk"].ToString().ToLower() == "1" || dr["IsTk"].ToString().ToLower() == "true")
                        {
                            formNames.Add(dr["ID"] + "_" + dr["SID"] + "_name");
                        }
                        index += 1;
                    }
                    this.hdFormName.Value += "," + string.Join(",", formNames.ToArray());
                    var dataview = TestSubject.Tables[1].AsDataView();

                    dataview.RowFilter = string.Format("SubjectID='{0}'", hiddenField.Value);
                    Repeater.DataSource = dataview;
                    Repeater.DataBind();
                    //divRs.Visible = false;
                }

            }
        }

        public string GetOpVisbile(object isTk)
        {
            if (isTk != DBNull.Value && (isTk.ToString().ToLower() == "1" || isTk.ToString().ToLower() == "true"))
            {
                return "1";
            }
            return "0";
        }

        #endregion

    }


}